from Case.python_ui.Common.basepage import Basepage
import time, pytest
from Case.python_ui.PageLocate.admin_coupon_voucher_records_locator import AdminCouponVoucherRecordsLocator
from selenium.common.exceptions import NoSuchElementException, ElementClickInterceptedException
from selenium.webdriver.common.by import By

class VoucherRecordsPage(Basepage):

    #判断是否包含元素
    def check_contain_str(self, string, except_string):
        if except_string in string:
            assert True
        else:
            assert False, '实际值:' + string + '期望值:' + except_string

    # 校验某个属性值是否符合预期
    def check_attribute(self, real_result, except_result):
        if real_result == except_result:
            assert True
        else:
            assert False, '实际值:' + real_result + '期望值:' + except_result

    # 元素是否存在
    def element_is_exist(self, locate):
        doc = '判断元素是否存在'
        try:
            self.get_element(locate, doc)
        except NoSuchElementException as e:
            return False
        else:
            return True

    #进入Voucher Records 菜单页面
    def enter_voucher_records_page(self):
        time.sleep(2)
        doc = "进入Voucher Records 菜单页面"
        js_object = 'document.getElementsByClassName("cre-sider-menu cre-sider-menu-open")[0].scrollTop=10000'
        self.excute_js(js_object)
        self.wait_elevisble(AdminCouponVoucherRecordsLocator.promo_management_menu, doc)
        self.click_element(AdminCouponVoucherRecordsLocator.promo_management_menu, doc)
        time.sleep(2)
        self.wait_elevisble(AdminCouponVoucherRecordsLocator.voucher_records_menu, doc)
        self.click_element(AdminCouponVoucherRecordsLocator.voucher_records_menu, doc)
        time.sleep(3)

    # 检查列表属性字段
    def check_voucher_records_list_attribute(self):
        time.sleep(2)
        self.check_list_attribute()

    # 清空筛选状态
    def clear_filter_status(self, locate):
        doc = '清空筛选状态'
        try:
            #点击筛选按钮
            self.click_element(locate, doc)
            time.sleep(2)
            #点击reset按钮
            self.click_element(AdminCouponVoucherRecordsLocator.status_filter_reset_button, doc)
        except ElementClickInterceptedException as e:
            self.click_element(AdminCouponVoucherRecordsLocator.status_filter_ok_button, doc)
            return
        else:
            self.click_element(AdminCouponVoucherRecordsLocator.status_filter_ok_button, doc)
            return

    # 清空筛选状态
    def clear_filter_selection(self, filter_button_locate):
        doc = '清空筛选状态'
        try:
            #点击筛选按钮
            self.click_element(filter_button_locate[0], doc)
            time.sleep(2)
            #点击reset按钮
            self.click_element(filter_button_locate[1], doc)
        except ElementClickInterceptedException as e:
            self.click_element(filter_button_locate[2], doc)
            return
        else:
            self.click_element(filter_button_locate[2], doc)
            return

    # 列表筛选功能
    def check_filter_result(self, search_num_locate, column_num, filter_button_locate, filter_arry):
        doc = '校验筛选结果'
        result_arry = []
        i = 1
        for selection in filter_arry:
            # 清空筛选条件
            self.clear_filter_status(filter_button_locate)
            time.sleep(2)
            filter_selection = self.assemble_filter_selection_locate(str(i))
            i = i + 1
            self.click_element(filter_button_locate, doc)
            time.sleep(2)
            # self.wait_elevisble(filter_selection, doc)
            self.click_element(filter_selection, doc)
            time.sleep(2)
            self.click_element(AdminCouponVoucherRecordsLocator.status_filter_ok_button, doc)
            time.sleep(5)
            if self.element_is_exist(search_num_locate) is False:
                result_arry.append(False)
                continue
            search_result = self.get_element(search_num_locate, doc).text
            search_num = int(search_result.split(' ', 2)[1])
            count = 0
            if search_num <= 10:
                for j in range(1, search_num + 1):
                    real_result = self.get_element(self.assemble_list_element_locate(str(j), str(column_num)), doc).text
                    self.check_attribute(real_result, selection)
            if search_num > 10:
                for j in range(1, 11):
                    real_result = self.get_element(self.assemble_list_element_locate(str(j), str(column_num)), doc).text
                    self.check_attribute(real_result, selection)
            result_arry.append(True)
        # 最后清空筛选条件
        self.clear_filter_status(filter_button_locate)
        return result_arry

    def assemble_list_element_locate(self, row, column):
        list_locate = '(//tr[contains(@class,"antd4-table-row antd4-table-row-level-0")])[$_1]//td[$_2]'
        search_result = (By.XPATH, list_locate.replace('$_1', row).replace('$_2', column))
        return search_result

    def assemble_filter_selection_locate(self, num):
        list_locate = '(//input[contains(@class,"antd4-checkbox-input")])[$_1]'
        status_selection_locate = (By.XPATH, list_locate.replace('$_1', num))
        return status_selection_locate

    # 列表筛选功能
    def check_filter_search_result(self, search_num_locate, column_num, filter_button_locate, filter_selection_locate, filter_value):
        doc = '校验筛选结果'
        #定义一个数组，当筛选项是没有查询结果时，存False
        result_arry = []
        #定义一个i, 用来取filter_value 的值
        i = 0
        for selection in filter_selection_locate:
            # 清空筛选条件
            self.clear_filter_selection(filter_button_locate)
            time.sleep(2)
            self.click_element(filter_button_locate[0], doc)
            time.sleep(2)
            self.click_element(selection, doc)
            time.sleep(2)
            self.click_element(filter_button_locate[2], doc)
            time.sleep(5)
            if self.element_is_exist(search_num_locate) is False:
                result_arry.append(False)
                i = i + 1
                continue
            search_result = self.get_element(search_num_locate, doc).text
            search_num = int(search_result.split(' ', 2)[1])
            if search_num <= 10:
                for j in range(1, search_num + 1):
                    real_result = self.get_element(self.assemble_list_element_locate(str(j), str(column_num)), doc).text
                    self.check_attribute(real_result, filter_value[i])
            if search_num > 10:
                for j in range(1, 11):
                    real_result = self.get_element(self.assemble_list_element_locate(str(j), str(column_num)), doc).text
                    self.check_attribute(real_result, filter_value[i])
            result_arry.append(True)
            i = i + 1
        # 最后清空筛选条件
        self.clear_filter_selection(filter_button_locate)
        return result_arry

    #搜索voucher records
    def search_voucher_records(self):
        time.sleep(2)
        doc = '搜索coupon records'
        fuzzy_query = ['lwm', 'wm_']
        self.send_keys(AdminCouponVoucherRecordsLocator.search_input, fuzzy_query[0], doc)
        time.sleep(2)
        self.click_element(AdminCouponVoucherRecordsLocator.search_button, doc)
        time.sleep(5)
        #检查是否能模糊查询
        self.check_contain_str(self.get_element(AdminCouponVoucherRecordsLocator.first_search_result_voucher_template, doc).text, fuzzy_query[0])
        time.sleep(2)
        #清空搜索框
        self.keyboard_event(AdminCouponVoucherRecordsLocator.search_input, doc)
        self.send_keys(AdminCouponVoucherRecordsLocator.search_input, fuzzy_query[1], doc)
        time.sleep(2)
        self.click_element(AdminCouponVoucherRecordsLocator.search_button, doc)
        time.sleep(5)
        #检查是否能模糊查询
        self.check_contain_str(self.get_element(AdminCouponVoucherRecordsLocator.first_search_result_voucher_template, doc).text, fuzzy_query[1])
        time.sleep(2)
        #获取第一条查询结果的，完整template 名称和 优惠券码
        doc = '获取第一条查询结果的template 名称和 优惠券码'
        coupon_no = self.get_element(AdminCouponVoucherRecordsLocator.first_search_result, doc).text
        coupon_template = self.get_element(AdminCouponVoucherRecordsLocator.first_search_result_voucher_template, doc).text
        doc = 'template 精确搜索'
        #清空搜索框
        self.keyboard_event(AdminCouponVoucherRecordsLocator.search_input, doc)
        self.send_keys(AdminCouponVoucherRecordsLocator.search_input, coupon_template, doc)
        time.sleep(2)
        self.click_element(AdminCouponVoucherRecordsLocator.search_button, doc)
        time.sleep(5)
        #检查是否能精确查询
        self.check_attribute(self.get_element(AdminCouponVoucherRecordsLocator.first_search_result_voucher_template, doc).text, coupon_template)
        time.sleep(2)
        doc = 'voucher no 精确搜索'
        #清空搜索框
        self.keyboard_event(AdminCouponVoucherRecordsLocator.search_input, doc)
        self.send_keys(AdminCouponVoucherRecordsLocator.search_input, coupon_no, doc)
        time.sleep(2)
        self.click_element(AdminCouponVoucherRecordsLocator.search_button, doc)
        time.sleep(5)
        #检查是否能精确查询
        self.check_attribute(self.get_element(AdminCouponVoucherRecordsLocator.first_search_result, doc).text, coupon_no)
        time.sleep(2)
        doc = 'voucher no 模糊搜索'
        #清空搜索框
        fuzzy_coupon_no = coupon_no[2:7]
        self.keyboard_event(AdminCouponVoucherRecordsLocator.search_input, doc)
        self.send_keys(AdminCouponVoucherRecordsLocator.search_input, fuzzy_coupon_no, doc)
        time.sleep(2)
        self.click_element(AdminCouponVoucherRecordsLocator.search_button, doc)
        time.sleep(5)
        #检查是否能模糊查询
        self.check_contain_str(self.get_element(AdminCouponVoucherRecordsLocator.first_search_result, doc).text, fuzzy_coupon_no)
        time.sleep(2)

    #筛选voucher records 列表
    def filter_voucher_records(self):
        time.sleep(2)
        doc = '筛选 voucher records'
        status_filter_value = ['Valid', 'Exhausted', 'Expired']
        status_filter_locate = [AdminCouponVoucherRecordsLocator.status_filter_valid_button, AdminCouponVoucherRecordsLocator.status_filter_exhausted_button, AdminCouponVoucherRecordsLocator.status_filter_expired_button]
        status_filter_button_locate = [AdminCouponVoucherRecordsLocator.status_filter_button, AdminCouponVoucherRecordsLocator.status_filter_reset_button, AdminCouponVoucherRecordsLocator.status_filter_ok_button]
        bind_filter_value = ['Y', 'N']
        bind_filter_locate = [AdminCouponVoucherRecordsLocator.bind_filter_y_button, AdminCouponVoucherRecordsLocator.bind_filter_n_button]
        bind_filter_button_locate = [AdminCouponVoucherRecordsLocator.bind_filter_button, AdminCouponVoucherRecordsLocator.bind_filter_reset_button, AdminCouponVoucherRecordsLocator.bind_filter_ok_button]
        self.check_filter_search_result(AdminCouponVoucherRecordsLocator.search_result_num, 2, status_filter_button_locate, status_filter_locate, status_filter_value)
        self.check_filter_search_result(AdminCouponVoucherRecordsLocator.search_result_num, 8, bind_filter_button_locate, bind_filter_locate, bind_filter_value)


    #校验列表元素
    #时区，货币单位需要单独提出来，根据配置文件来校验  --待优化
    def check_list_attribute(self):
        time.sleep(2)
        doc = "校验列表元素"
        attribute_list = ['Voucher No.', 'Status', 'Voucher Template', 'Account Email', 'Fixed Amount', 'Balance', 'Reuse', 'Bind', 'Validation Period']
        list_attribute_0 = self.get_element(AdminCouponVoucherRecordsLocator.record_list_voucher_no, doc).text
        self.check_contain_str(list_attribute_0, attribute_list[0])
        list_attribute_1 = self.get_element(AdminCouponVoucherRecordsLocator.record_list_status, doc).text
        self.check_contain_str(list_attribute_1, attribute_list[1])
        list_attribute_2 = self.get_element(AdminCouponVoucherRecordsLocator.record_list_voucher_template, doc).text
        self.check_contain_str(list_attribute_2, attribute_list[2])
        list_attribute_3 = self.get_element(AdminCouponVoucherRecordsLocator.record_list_account_email, doc).text
        self.check_contain_str(list_attribute_3, attribute_list[3])
        list_attribute_4 = self.get_element(AdminCouponVoucherRecordsLocator.record_list_fixed_amount, doc).text
        self.check_contain_str(list_attribute_4, attribute_list[4])
        list_attribute_5 = self.get_element(AdminCouponVoucherRecordsLocator.record_list_balance, doc).text
        self.check_contain_str(list_attribute_5, attribute_list[5])
        list_attribute_6 = self.get_element(AdminCouponVoucherRecordsLocator.record_list_reuse, doc).text
        self.check_contain_str(list_attribute_6, attribute_list[6])
        list_attribute_7 = self.get_element(AdminCouponVoucherRecordsLocator.record_list_bind, doc).text
        self.check_contain_str(list_attribute_7, attribute_list[7])
        list_attribute_8 = self.get_element(AdminCouponVoucherRecordsLocator.record_list_validation_period, doc).text
        self.check_contain_str(list_attribute_8, attribute_list[8])
        time.sleep(3)